package com.tang.companyjob.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tang.common.entity.companyJob.Job;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;

import com.tang.companyjob.service.JobService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/job")
public class JobController {
    @Autowired
    private JobService jobService;
    @ApiOperation("职位列表")
    @RequestMapping("/jobList")
    public Result companyList(String keywords, Integer currentPage, Integer pageSize) {
        PageInfo<Job> list = PageInfo.of(jobService.list(keywords,currentPage,pageSize));
        return Result.success(list);
    }
    @RequestMapping("/companyJob")
    public Result getCompanyJob(String keywords, Integer currentPage, Integer pageSize,String companyName){
        PageInfo<Job> list = PageInfo.of(jobService.listByCompanyName(keywords,currentPage,pageSize,companyName));
        return Result.success(list);
    }

    @ApiOperation("职位列表")
    @RequestMapping("/jobBeginList")
    public Result jobBeginList(String keywords, Integer currentPage, Integer pageSize) {
        PageInfo<Job> list = PageInfo.of(jobService.listBegin(keywords,currentPage,pageSize));
        return Result.success(list);
    }
    @ApiOperation("上架下架职位")
    @PutMapping("/shelveJob")
    public Result shelveJob(int jobId) {
        int result = jobService.shelveByJobId(jobId);
        if (result != 0) {
            return Result.success("操作成功");
        } else {
            return Result.error(RecruitExceptionEnum.HANDLE_FAILED);
        }
    }
    @ApiOperation("删除职位")
    @RequestMapping("/delJob")
    public Result delCompany(Integer jobId){
        int result = jobService.deleteByJobId(jobId);
        if (result != 0) {
            return Result.success("删除成功");
        } else {
            return Result.error(RecruitExceptionEnum.DELETE_FAILED);
        }
    }
    @ApiOperation("获取")
    @RequestMapping("/getJob")
    public Result getJob(Integer jobId){
        Job job = jobService.getJob(jobId);
        if(job!=null){
            return Result.success(job);
        }else {
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }

    @GetMapping("/getFeignJob")
    public Job getFeignJob(@RequestParam("jobId")  Integer jobId){
        Job job = jobService.getJob(jobId);
        if(job!=null){
            return job;
        }
      return null;
    }

    @RequestMapping("/jobCount")
    public Integer getJobCount(){
        return jobService.getJobCount();
    }

    @RequestMapping("/updateJob")
    public Result updateJob(@RequestParam("infoForm") @RequestBody String infoForm){
        Job job = JSON.parseObject(infoForm, Job.class);
        if(job!=null){
            boolean b = jobService.updateById(job);
            if(b){
                return Result.success("修改成功");
            }
        }
        return Result.error(RecruitExceptionEnum.UPDATE_FAILED);
    }


    @RequestMapping("/addJob")
    public Result addJob(@RequestParam("infoForm") @RequestBody String infoForm){
        Job job = JSON.parseObject(infoForm, Job.class);
        if(job!=null){
            boolean save = jobService.save(job);
            if(save){
                return Result.success("添加成功");
            }
        }
        return Result.error(RecruitExceptionEnum.ADD_FAILED);
    }
}
